import { _decorator, Button, Component, game, Label, Node, ProgressBar, sys } from 'cc';
import HotUpdateScript, { CheckCallbackType, UpdateCallbackType } from './HotUpdateScript';
import JoinGameProcessCmd from './JoinGameProcessCmd';
import { GlobelMgr } from '../../managers/GlobelMgr';
import { SceneName } from '../../managers/GlobelDataMgr';
const { ccclass, property } = _decorator;

@ccclass('LandUI')
/**登陆游戏界面*/
export class LandUI extends Component {
    /**热更新脚本*/
    @property(HotUpdateScript)
    hotUpdateScript:HotUpdateScript;
    @property(ProgressBar)
    progressBar:ProgressBar;
    @property(ProgressBar)
    gameProgressBar:ProgressBar;
    @property(Label)
    progressBarText:Label;
    @property(Label)
    versionTxt:Label;
    @property(Button)
    updateBtn:Button;
    start() {
        this.progressBar.node.active = false;
        this.updateBtn.node.active = false;
        this.gameProgressBar.node.active = false;
    }
    public startScript():void{
        if(!sys.isNative)
        {
            this.joinGame();
        }else{
            this.hotUpdateScript.startScript();
        }
    }
    private joinGame():void{
        this.progressBar.node.active = false;
        this.gameProgressBar.node.active = true;
        let joinCmd:JoinGameProcessCmd = new JoinGameProcessCmd();
        joinCmd.start(this.setProgression.bind(this),this.joinGameOver);
    }
    private joinGameOver():void{
        GlobelMgr.instance.switchingScenes(SceneName.hall);
    }
    private startHotUpdate():void{
        this.hotUpdateScript.hotUpdate();
        this.progressBar.node.active = true;
    }
    //进入游戏的进度条
    private setProgression(percent:number):void{
        this.gameProgressBar.progress = percent;
    }
    //检测热更新返回
    onCheckCallbackEvent(e:CheckCallbackType){
        switch(e)
        {
            case CheckCallbackType.ERROR_NO_LOCAL_MANIFEST:
                this.joinGame();
            break;

            case  CheckCallbackType.ERROR_PARSE_MANIFEST:
                this.joinGame();
            break;

            case CheckCallbackType.ALREADY_UP_TO_DATE:
                this.joinGame();
            break;

            case CheckCallbackType.NEW_VERSION_FOUND:
                this.updateBtn.node.active = true;
            break;
        }
    }
    //热更新进度调
    onUpdateProgressionEvent(percent:number,msg){
        this.progressBarText.string = msg+":"+percent*100+"%";
        this.progressBar.progress = percent;
    }
    //监听热更新是否成功
    onUpdateCallbackEvent(type:UpdateCallbackType){
        switch(type)
        {
            case UpdateCallbackType.UPDATE_FINISHED:
                game.restart();
            break;

            case UpdateCallbackType.ALREADY_UP_TO_DATE:
                this.joinGame();
            break;

            case UpdateCallbackType.ERROR_NO_LOCAL_MANIFEST:
                this.joinGame();
            break;

            case UpdateCallbackType.FAIL:
                this.joinGame();
            break;
        }
    }
    onLocalVersionEvent(version:string){
        this.versionTxt.string = version;
    }
    onClick(e,data:string):void{
        switch(data)
        {
            case "update":
                this.startHotUpdate();
            break;
        }
    }

}


